home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / AcceptWindow.m < prev    next >
Text File  |  1993-01-12  |  6KB  |  219 lines

  1. // modifided from AccpetWindow.m in NextDeveloper/Examples/WhatADrag
  2. //  by Miyai, ISR    1992.7.28
  3.  
  4. // AcceptWindow.m
  5. // By Jayson Adams, NeXT Developer Support Team
  6. // You may freely copy, distribute and reuse the code in this example.
  7. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  8. // fitness for any particular use.
  9.  
  10. #import <stdlib.h>
  11. #import <objc/Storage.h>
  12.  
  13. #import "AcceptWindow.h"
  14. #import "IFolderCell.h"
  15. #import "TransparentWindow.h"
  16. #import "errdebug.h"
  17.  
  18. #define NOVIEWRECT -1
  19.  
  20. typedef struct _ViewRectPair {
  21.     id    view;
  22.     id    controlView;
  23.     NXRect    rect;
  24.     } ViewRectPair;
  25.  
  26.  
  27. @implementation AcceptWindow
  28.  
  29.  
  30. /* instance methods */
  31.  
  32. - initContent:(const NXRect *)contentRect style:(int)aStyle
  33.   backing:(int)bufferingType buttonMask:(int)mask defer:(BOOL)flag
  34. {
  35.     [super initContent:contentRect
  36.        style:aStyle
  37.        backing:bufferingType
  38.        buttonMask:mask
  39.        defer:flag];
  40.  
  41.     viewRectList = [[Storage alloc] initCount:0
  42.                     elementSize:sizeof(ViewRectPair)
  43.                     description:"{@@ffff}"];
  44.     
  45.     viewRectUnderPoint = NOVIEWRECT;
  46.     
  47.     return self;
  48. }
  49.  
  50. - registerRect:(NXRect *)rect forCell:cell controlView:cView
  51. {
  52.     ViewRectPair    *newViewRect, *viewRect;
  53.     int        listCount;
  54.     
  55.     // check if the same cell is already registered,
  56.     // and if it registered, remove it from view list
  57.     listCount = [viewRectList count];
  58.     while (listCount--) {
  59.     // element num. is (listCount-1)
  60.     viewRect = [viewRectList elementAt:listCount];
  61.     if ((viewRect->view) == cell) {
  62.         //[viewRectList removeAt:listCount];
  63.         [viewRectList removeElementAt:listCount];
  64.         DBG(10,fprintf(stderr," remove old viewRect from list for cell that is already registered and try to register new one.\n"));
  65.         break;
  66.     }
  67.     }
  68.     
  69.   /* add the view-rect pair to our list */
  70.     newViewRect = (ViewRectPair *)malloc(sizeof(ViewRectPair));
  71.     newViewRect->view = cell;
  72.     newViewRect->controlView = cView;
  73.     newViewRect->rect = *rect;
  74.     
  75.     [viewRectList addElement:newViewRect];
  76.     
  77.     return self;
  78. }
  79.  
  80. - unregisterRectForCell:cell
  81. {
  82.     int        listCount;
  83.     ViewRectPair    *viewRect;
  84.  
  85.     listCount = [viewRectList count];
  86.     while (listCount--) {
  87.     viewRect = [viewRectList elementAt:listCount];
  88.     if ((viewRect->view) == cell) {
  89.         //[viewRectList removeAt:listCount];
  90.         [viewRectList removeElementAt:listCount];
  91.         return (cell);
  92.     }
  93.     }
  94.     DBG(10,fprintf(stderr," couldn't find cell to be unregisterd\n"));
  95.     return nil;
  96. }
  97.  
  98. - (BOOL)windowEntered:(NXPoint *)mouseLocation fromSource:dragSource
  99. {
  100.     NXPoint        windowPoint;
  101.     int            newViewRectUnderPoint;
  102.     ViewRectPair    *viewRect, *oldViewRect;
  103.     BOOL        oldViewDrew = NO, newViewDrew = NO;
  104.         
  105.   /* convert the mouse location to local coordinates */
  106.     windowPoint = *mouseLocation;
  107.     [self convertScreenToBase:&windowPoint];
  108.     
  109.   /* see if any of the views in our list lay under the mouse point */
  110.     newViewRectUnderPoint = [viewRectList count];
  111.     while (newViewRectUnderPoint--) {
  112.     viewRect = (ViewRectPair *)[viewRectList
  113.                               elementAt:newViewRectUnderPoint];
  114.     if (NXPointInRect(&windowPoint, &(viewRect->rect))) {
  115.         break;
  116.     }
  117.     }
  118.     
  119.   /* see if the mouse has moved over a different view */
  120.     if (viewRectUnderPoint != newViewRectUnderPoint) {
  121.       /* tell the view previously under the mouse that the window has exited */
  122.     if (viewRectUnderPoint != NOVIEWRECT) {
  123.         oldViewRect = (ViewRectPair *)[viewRectList
  124.                          elementAt:viewRectUnderPoint];
  125. //        oldViewDrew = [oldViewRect->view windowExited:dragSource];
  126.         oldViewDrew = [oldViewRect->view windowExited:dragSource
  127.                         controlView:oldViewRect->controlView];
  128.     }
  129.       /* save the new view under the mouse */
  130.     viewRectUnderPoint = newViewRectUnderPoint;
  131.     
  132.       /*
  133.        * if there's a view under the mouse point, tell it that a window has
  134.        * entered it
  135.        */
  136.     if (viewRectUnderPoint != NOVIEWRECT) {
  137.         viewRect = (ViewRectPair *)[viewRectList
  138.                             elementAt:viewRectUnderPoint];
  139. //        newViewDrew = [viewRect->view windowEntered:dragSource];
  140.         newViewDrew = [viewRect->view windowEntered:dragSource
  141.                         controlView:viewRect->controlView];
  142.     }
  143.     }
  144.     
  145.   /* let the caller know if any of our accept views has done any drawing */
  146.     return (oldViewDrew || newViewDrew);
  147. }
  148.  
  149. - (BOOL)windowExited:dragSource
  150. {
  151.     ViewRectPair    *viewRect;
  152.     BOOL        viewDrew = NO;
  153.     
  154.   /* tell any view previously under the mouse point that it no longer is */
  155.     if (viewRectUnderPoint != NOVIEWRECT) {
  156.     viewRect = [viewRectList elementAt:viewRectUnderPoint];
  157. //    viewDrew = [viewRect->view windowExited:dragSource];
  158.     viewDrew = [viewRect->view windowExited:dragSource 
  159.                     controlView:viewRect->controlView];
  160.     viewRectUnderPoint = NOVIEWRECT;
  161.     }
  162.     
  163.   /* tell the caller whether or not the view did some drawing */
  164.     return viewDrew;
  165. }
  166.  
  167. - (BOOL)windowDropped:(NXPoint *)mouseLocation fromSource:source
  168. {
  169.     NXPoint        windowPoint;
  170.     int            newViewRectUnderPoint;
  171.     ViewRectPair    *viewRect;
  172. //    BOOL        accepted = NO;
  173.     BOOL        accepted;
  174.     
  175.     accepted = [source defaultAccepted];
  176.     
  177.   /* convert the mouse location to local coordinates */
  178.     windowPoint = *mouseLocation;
  179.     [self convertScreenToBase:&windowPoint];
  180.     
  181.   /* see if any of the views in our list lay under the mouse point */
  182.     newViewRectUnderPoint = [viewRectList count];
  183.     while (newViewRectUnderPoint--) {
  184.     viewRect = (ViewRectPair *)[viewRectList
  185.                              elementAt:newViewRectUnderPoint];
  186.     if (NXPointInRect(&windowPoint, &(viewRect->rect))) {
  187.         break;
  188.     }
  189.     }
  190.  
  191.   /* tell any view previously under the mouse point that the window moved */
  192.     if (viewRectUnderPoint != NOVIEWRECT &&
  193.     viewRectUnderPoint != newViewRectUnderPoint) {
  194.     
  195.     viewRect = (ViewRectPair *)[viewRectList elementAt:viewRectUnderPoint];
  196. //    [viewRect->view windowExited:source];   
  197.     [viewRect->view windowExited:source controlView:viewRect->controlView];   
  198.     }
  199.     
  200.   /*
  201.    * tell the view under the mouse point (if any) that the user dropped a
  202.    * window on it
  203.    */
  204.     if (newViewRectUnderPoint != NOVIEWRECT) {
  205.     viewRect = (ViewRectPair *)[viewRectList
  206.                                elementAt:newViewRectUnderPoint];
  207. //    accepted = [viewRect->view windowDropped:source];
  208.     accepted = [viewRect->view windowDropped:source 
  209.                 controlView:viewRect->controlView];
  210.     }
  211.     
  212.   /* no window under the mouse now */
  213.     viewRectUnderPoint = NOVIEWRECT;
  214.     
  215.     return accepted;
  216. }
  217.  
  218. @end
  219.